package ph.com.gs3.formalistics.presenter.fragments.workers;

import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.DatabaseHelper;
import ph.com.gs3.formalistics.model.communicators.CommentCommunicatorModule;
import ph.com.gs3.formalistics.model.communicators.HttpCommunicator;
import ph.com.gs3.formalistics.model.communicators.HttpCommunicatorModule.CommunicatorException;
import ph.com.gs3.formalistics.model.communicators.NotificationCommunicatorModule;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse.APIInvalidResponseException;
import ph.com.gs3.formalistics.model.valueobjects.business.User;
import ph.com.gs3.formalistics.service.managers.SessionManager;
import android.app.Fragment;
import android.content.Context;
import android.os.AsyncTask;

public class DeveloperOptionsWorkerFragment extends Fragment {

	public static final String TAG = DeveloperOptionsWorkerFragment.class.getSimpleName();

	public void resetDatabase() {
		Context context = getActivity();
		context.deleteDatabase(DatabaseHelper.DATABASE_NAME);
	};

	public void testCommentCommModule() {

		AsyncTask<Void, Void, Void> testTask = new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... arg0) {

				User activeUser = SessionManager.getApplicationInstance()
				        .getLastActiveUserFromDatabase();

				String testServer = activeUser.getAccountServer();
				HttpCommunicator communicator = new HttpCommunicator();

				CommentCommunicatorModule commentCommModule = new CommentCommunicatorModule(
				        communicator, testServer);

				String testFormId = "1";
				String[] testDocumentIdList = { "1", "2", "3" };
				String testLastUpdateDate = "2014-10-08 15:00:40";

				int rangeFrom = 1;
				int rangeTo = 1;

				try {
					commentCommModule.getFormDocumentsCommentUpdates(testFormId,
					        testDocumentIdList, testLastUpdateDate, rangeFrom, rangeTo);
				} catch (CommunicatorException | APIInvalidResponseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return null;
			}

		};

		testTask.execute();
		FLLogger.d(TAG, "Now testing comment communicator module.");

	}

	public void testNotifications() {

		AsyncTask<Void, Void, Void> testTask = new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {

				User activeUser = SessionManager.getApplicationInstance()
				        .getLastActiveUserFromDatabase();

				String testServer = activeUser.getAccountServer();
				HttpCommunicator communicator = new HttpCommunicator();

				NotificationCommunicatorModule communicatorModule = new NotificationCommunicatorModule(
				        communicator, testServer);

				try {
					APIResponse response = communicatorModule.getNotifications(0, 10);
					FLLogger.d(TAG, response.getResults());
				} catch (CommunicatorException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (APIInvalidResponseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				return null;
			}

		};

		testTask.execute();

	}

}
